package gdou.gym.web.admin;

import com.github.pagehelper.PageInfo;
import gdou.gym.entity.*;
import gdou.gym.exception.StateCode;
import gdou.gym.exception.SysException;
import gdou.gym.returnvo.UserQueryInfoVo;
import gdou.gym.service.admin.AdminService;
import gdou.gym.service.equipment.EqService;
import gdou.gym.service.order.OrderService;
import gdou.gym.service.space.SpaceService;
import gdou.gym.service.user.UserService;
import gdou.gym.sys.log.Log;

import gdou.gym.token.AllowAccessFor;
import gdou.gym.token.Authorization;
import gdou.gym.token.TokenUtil;
import gdou.gym.vo.*;
import gdou.gym.web.MySessionListen;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;


@RestController
@RequestMapping("/admin")
@CrossOrigin
public class AdminController {

    private static final ThreadLocal<SimpleDateFormat> THREAD_LOCAL
            = ThreadLocal.withInitial(() -> new SimpleDateFormat("yyyy-MM-dd"));

    private static final ConcurrentHashMap<String, HttpSession> MAP = new ConcurrentHashMap<>();
    @Autowired
    private UserService userService;
    @Autowired
    private AdminService adminService;
    @Autowired
    private OrderService orderService;
    @Autowired
    private SpaceService spaceService;
    @Autowired
    private EqService eqService;

    @PostMapping("/login")
    public Map<String, Object> adminLogin(@RequestBody Admin admin) {
        Map<String, Object> map = new HashMap<>();
        Admin a = adminService.checkLogin(admin);
        String token = TokenUtil.getToken(a);
        MySessionListen.put(token);
        map.put("token", token);
        map.put("account", a.getAccount());
        map.put("name", a.getAccount());
        return map;
    }

    @GetMapping("/eq")
    public List<Eqment> queryEq() {
        return eqService.query();
    }

    @PostMapping("/equpdate")
    @AllowAccessFor(Authorization.ADMIN)
    public void update(@RequestBody Eqment eqment) {
        eqService.updateEq(eqment);
    }


    @GetMapping("/equpdate")
    @AllowAccessFor(Authorization.ADMIN)
    public void removeEq(
            @RequestParam("no") String no) {
        eqService.delEq(no);
    }

    @GetMapping("/addAdmin")
    public void addAdmin(Admin admin) {
        adminService.addAdmin(admin);
    }

    @GetMapping("/userSearch")
    public PageInfo<User> queryUser(
            @Page(clazz = User.class)
                    MyPageInfo<User> info) {
        List<User> users = userService.queryByCondition(info);
        return new PageInfo<>(users);
    }

    @AllowAccessFor({Authorization.ADMIN})
    @GetMapping("/reason")
    public Map<String, String> getDiscreditReason(String account) {
        if (account == null) {
            throw new SysException(StateCode.PARAMS_ERROR);
        }
        String reason = userService.getDiscreditReason(account);
        if (reason == null) {
            throw new SysException(StateCode.NoDiscover_ERROR);
        }
        Map<String, String> map = new HashMap<>();
        if ("".equals(reason.trim()))
            reason = "封禁原因未知";
        map.put("reason", reason);
        return map;
    }


    @GetMapping("/queryOrder")
    public Map<String, Object> queryOrder(@Page(clazz = AdminQueryOrderVo.class) MyPageInfo<AdminQueryOrderVo> info) {
        return orderService.queryAllOrderByCondition(info);
    }

    @GetMapping("/edit")
    @AllowAccessFor({Authorization.ADMIN})
    @Log
    public void editUser(String account, String reason) {
        userService.upDataUser(account, reason);
    }


        @GetMapping("/queryOrderSpaceMsg")
    @AllowAccessFor({Authorization.ADMIN})
    public List<?> queryOrderSpaceMsg() {
        return new ArrayList<>(orderService.queryOrderSpaceMsg());

    }

    @GetMapping("/queryOrderSpaceCostMsg")
    @AllowAccessFor({Authorization.ADMIN})
    public List<AdminQueryOrderSpaceCostVo> queryOrderSpaceCostMsg() {
        return orderService.queryOrderSpaceCostMsg();
    }

    @GetMapping("/queryRecentOrderMsg")
    @AllowAccessFor({Authorization.ADMIN})
    public Map<String, Double> recentOrderMsg(Integer nums) {
        String curDate = THREAD_LOCAL.get().format(new Date());
        return orderService.searchRecentOrder(curDate, nums == null ? 7 : nums);
    }

    @GetMapping("/addSpace")
    @AllowAccessFor(Authorization.ADMIN)
    public void addSpace(SpaceAndTimeVo vo) {
        spaceService.addNewSpace(vo);
    }

    @GetMapping("/searchSpace")
    public PageInfo<SpaceView> search(
            @Page(clazz = UserQueryInfoVo.class)
                    MyPageInfo<UserQueryInfoVo> info) {
        List<SpaceView> space = spaceService.queryByCondition(info, false);
        return new PageInfo<>(space);
    }

    @GetMapping("/editRemain")
    @AllowAccessFor(Authorization.ADMIN)
    public void editRemain(Integer id, Integer remain) {
        spaceService.updateSpaceRemain(id, remain);
    }

    @GetMapping("/removeSpace")
    @AllowAccessFor(Authorization.ADMIN)
    public void removeSpace(Integer id) {
        spaceService.removeSpaceById(id);
    }


    @GetMapping("/ask")
    public PageInfo<SiteApplication> queryAskByCondition(@Page(clazz = Ask4SpaceVo.class)
                                                                 MyPageInfo<Ask4SpaceVo> vo) {
        return spaceService.queryAskByCondition(vo);
    }
}
